package com.food.dao;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.food.dao.in.StepDAO;
import com.food.model.Step;

public class StepDAOJdbc implements StepDAO {
//	private static final String URL = "jdbc:sqlserver://192.168.34.108:1433;database=PJDB";
//	private static final String USER = "sa";
//	private static final String PASSWORD = "sa123456";
	
	private DataSource ds = null;
	public StepDAOJdbc() {
		try {
			Context ctx = new InitialContext();
			this.ds = (DataSource) ctx.lookup("java:comp/env/jdbc/food");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	private static final String INSERT ="insert into step (rid, snumber, sword, simg) values(?,?,?,?)";
	@Override
	public void insertStep(Step bean) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(INSERT);
			pstmt.setInt(1, bean.getRid());
			pstmt.setInt(2, bean.getSnumber());
			pstmt.setString(3, bean.getSword());
			pstmt.setBinaryStream(4, bean.getIs(), bean.getSize());
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		

	}
	private static final String UPDATE ="update step set rid=?, snumber=?, sword=? , simg=?  where sid =?";
	@Override
	public void updateStep(int rid, int snumber, String sword, byte[] simg,
			int sid) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(UPDATE);
			pstmt.setInt(1, rid);
			pstmt.setInt(2, snumber);
			pstmt.setString(3, sword);
			pstmt.setBytes(4, simg);
			pstmt.setInt(5, sid);
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
	private static final String DELETE ="delete from step where sid =?";
	@Override
	public void deleteStep(int sid) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(DELETE);
			pstmt.setInt(1, sid);
			pstmt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}

	}
	private static final String SELECT ="select sid ,rid, snumber, sword , simg from step where rid=?";
	@Override
	public List<Step> findStepByRecipe(int rid) {
		List<Step> result = null;
		Step bean = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
//			conn = DriverManager.getConnection(URL, USER, PASSWORD);
			conn = this.ds.getConnection();
			pstmt = conn.prepareStatement(SELECT);
			pstmt.setInt(1, rid);
			rs = pstmt.executeQuery();
			result = new ArrayList<Step>();
			while(rs.next()){
				bean = new Step();
				bean.setSid(rs.getInt("sid"));
				bean.setRid(rs.getInt("rid"));
				bean.setSnumber(rs.getInt("snumber"));
				bean.setSword(rs.getString("sword"));
				bean.setSimg(rs.getBytes("simg"));
				result.add(bean);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if (rs!=null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (pstmt!=null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (conn!=null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	
	public static void main(String[] args){
//		StepDAOJdbc dao = new StepDAOJdbc();
//		Step s = new Step();
//		s.setRid(1);
//		s.setSnumber(1);
//		s.setSword("�[�J");
//		dao.insertStep(s);
//		dao.updateStep(1, 1, "�[�J�J", null,1);

//		System.out.println(dao.findStepByRecipe(1));
	
	}

}
